/**
 * <strong>Title : EquipmentStatusServiceImpl.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.pp.service.impl<br>
 * </strong> <strong>Description : </strong>@类注释说明写在此处@<br>
 * <strong>Create on : 2015年11月2日 上午10:14:46<br>
 * </strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
package cn.b.sky.mes.em.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.em.dao.EquipmentStatusDao;
import cn.b.sky.mes.em.model.EquipmentStatus;
import cn.b.sky.mes.em.model.condition.EquipmentStatusCondition;
import cn.b.sky.mes.em.service.EquipmentStatusService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

/**
 * <strong>Title : EquipmentStatusServiceImpl.java<br></strong>
 * <strong>Package : cn.b.sky.mes.pp.service.impl<br></strong>
 * <strong>Description : </strong>设备状态业务实现<br> 
 * <strong>Create on : 2015年11月2日 上午10:14:46<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br> 
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Service("equipmentStatusService")
public class EquipmentStatusServiceImpl implements EquipmentStatusService {

  private static final Logger LOG = LoggerFactory.getLogger(EquipmentStatusServiceImpl.class);

  @Resource
  EquipmentStatusDao equipmentStatusDao;

  @Override
  public List<EquipmentStatus> selectEquipmentStatusList(EquipmentStatusCondition condition) {
    if (condition == null) {
      condition = new EquipmentStatusCondition();
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("CreateDateTime");
    }
    return equipmentStatusDao.selectEquipmentStatusList(condition);
  }

  @Override
  public int selectEquipmentStatusCount(EquipmentStatusCondition condition) {
    if (condition == null) {
      condition = new EquipmentStatusCondition();
    }
    return equipmentStatusDao.selectEquipmentStatusCount(condition);
  }

  @Override
  public EquipmentStatus getEquipmentStatus(String id, String siteCode) {
    if (StringUtils.isBlank(id)) {
      return null;
    }

    return equipmentStatusDao.getEquipmentStatus(id, siteCode);
  }

  @Override
  public void updateEquipmentStatus(EquipmentStatus equipmentStatus) {
    if (equipmentStatus == null) {
      return;
    }
    equipmentStatusDao.updateEquipmentStatus(equipmentStatus);

  }

  @Override
  public void saveEquipmentStatus(EquipmentStatus equipmentStatus) {
    if (equipmentStatus == null) {
      return;
    }
    equipmentStatusDao.saveEquipmentStatus(equipmentStatus);

  }

  @Override
  public BatchJobReport deleteEquipmentStatus(String statusCode, String siteCode) {
    String message = statusCode;
    String cause = "";
    boolean success = true;
    try {

      equipmentStatusDao.deleteEquipmentStatus(statusCode, siteCode);

    } catch (Exception e) {
      LOG.error("{}", e);
      success = false;
      String errorMsg = e.getMessage();
      if (DatabaseUtils.isConstraintsError(e.getMessage())) {
        //外键删除已存在提示
        errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
            SecurityContextUtil.getLanguageCode(), "dataBaseOperationPkFail.M")
            + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
                SecurityContextUtil.getLanguageCode(), "notDelete.M");
      }
      cause = errorMsg;
    }
    return new BatchJobReport(message, cause, success);
  }

  @Override
  public List<EquipmentStatus> selectEquipmentStatusForSuggest(EquipmentStatusCondition condition) {
    return equipmentStatusDao.selectEquipmentStatusForSuggest(condition);
  }

}
